<?php

/**
 * uniBoard ACP New Rank
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_rank{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_user_rank( $appCore, $appCore -> _REQ( 'rank'));
			
		// Api loaded rank?
		if ( !$this -> api -> data_loaded)
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['acp_rank_not_found'], $appCore -> lang['acp_users_ranks']);
			
			// Jump back to Ranks List
			new action_ranks( $appCore);
		}
		else if ( $appCore -> _REQ( 'do') == 'edit')
		{
			// Update?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Fill Api
				$this -> api -> setRankSpecial( $appCore -> _POST( 'rank_special'));
				$this -> api -> setRankPosts( $appCore -> _POST( 'rank_posts'));
				$this -> api -> setRankName( $appCore -> _POST( 'rank_name'));
				$this -> api -> setRankImage( $appCore -> _POST( 'rank_image'));
				$this -> api -> setRankStars( $appCore -> _POST( 'rank_stars'));
				
				// Api All-rite?
				if ( $this -> api -> error)
				{
					// Draw Errors
					$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> ifString( htmlspecialchars( $this -> api -> rank_name)));
					
					// Repeat Form
					$this -> drawForm();
				}
				else
				{
					// Save Api
					$this -> api -> saveData();
					
					// Set Log
					$appCore -> logAdmin( 'log_admin_edited_user_rank', htmlspecialchars( $appCore -> _POST( 'rank_name')));
					
					// Draw Message
					$appCore -> output -> message( $appCore -> lang['acp_rank_edited'], $appCore -> ifString( htmlspecialchars( $this -> api -> rank_name)));
					
					// Jump back to Ranks List
					new action_ranks( $appCore);
				}
			}
			else
			{
				// Draw Form
				$this -> drawForm();
			}
		}
		else if ( $appCore -> _REQ( 'do') == 'delete')
		{
			// Delete Api
			$this -> api -> deleteData();
			
			// Set Log
			$appCore -> logAdmin( 'log_admin_deleted_user_rank', htmlspecialchars( $appCore -> _POST( 'rank_name')));
			
			// Draw Message
			$appCore -> output -> message( $appCore -> lang['acp_rank_deleted'], $appCore -> ifString( htmlspecialchars( $this -> api -> rank_name)));
			
			// Jump back to Ranks List
			new action_ranks( $appCore);	
		}
		else
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['acp_users_ranks']);
			
			// Jump back to Ranks List
			new action_ranks( $appCore);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_rank&do=edit&rank=' . $this -> api -> rank_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_rank_form_sect_settings']);
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_rank_form_special'], $this-> appCore -> drawYesNo( 'rank_special', $this -> api -> rank_special), $this -> appCore -> lang['acp_rank_form_special_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_rank_form_name'], $this-> appCore -> drawTextInput( 'rank_name', $this -> api -> rank_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_rank_form_posts'], $this-> appCore -> drawTextInput( 'rank_posts', $this -> api -> rank_posts), $this -> appCore -> lang['acp_rank_form_posts_expl']);
		
		// New block
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_rank_form_sect_images']);
		
		// Draw two more fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_rank_form_image'], $this-> appCore -> drawTextInput( 'rank_image', $this -> api -> rank_image), $this -> appCore -> lang['acp_rank_form_image_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_rank_form_stars'], $this-> appCore -> drawTextInput( 'rank_stars', $this -> api -> rank_stars), $this -> appCore -> lang['acp_rank_form_stars_expl']);
				
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['edit_rank_form'], array(
			$this -> appCore -> adminLink( 'act=ranks') => $this -> appCore -> lang['acp_users_ranks'],
			'' => $this -> appCore -> ifString( htmlspecialchars( $this -> api -> rank_name))
		), ob_get_clean());
			
	}

}